<?php
namespace app\admin\controller;
// use app\index\
class Cases extends Base
{
    public function index()
    {
        $fid=request()->param('fid');
        // print_r($cid);
        $keywords=request()->param('keywords');       
        $article=db("channel_cases");
        $channel=db("channel");
        $where['status']=1;
        $where['cid']=$fid;
        if($keywords){
            $where['title']=['like', '%'.$keywords.'%'];
        }
        // print_r($where);exit;
        // $where['type']=1;
        $count=$article->where($where)->count();
        $data=$article->where($where)->order("is_top desc,sort asc,id desc")->select();

        $fname=db("channel")->where("id",$fid)->value("name");
        $this->assign("fname",$fname);
        $this->assign("fid",$fid);
        $this->assign("mess",$data);
        $this->assign("count",$count);
        return view('index');
    }
    public function index_del(){
        $id=$_GET['id'];
        $res=db("channel_cases")->delete($id);
        if($res){
            $mess['code']=1;
            $mess['msg']="删除成功";
        }else{
            $mess['code']=0;
            $mess['msg']="删除失败";
        }       // print_r($data);exit;
        return json_encode($mess);
    }
    public function addMess()
    {
        // print_r(strtotime("2018-06-15 00:00:00"));
        // print_r(date("Y-m-d H:i:s",1528992000));exit;
        $id=request()->param('id');
        $fid=request()->param('fid');
        if($id){
            $mess=db("channel_cases")->where("id",$id)->find();

            $where['status']=1;
            $where['tid']=$mess['id'];
            $where['tablename']="channel_cases";
            $mess['photos']=db("attachment")->where($where)->select();
            $images=db("attachment")->where($where)->column("id");
            $mess['images']=implode(",", $images);
            $mess['createtime']=date("Y-m-d H:i:s",$mess['createtime']);
            $this->assign("mess",$mess);
        }
        
        $this->assign("fid",$fid);
        return view('addMess');
    }
    public function addMessDone()
    {
        $updid=request()->param('updid'); 
        $data['title']=$_POST['title'];
        $data['cid']=$_POST['cid'];
        $data['thumb']=$_POST['thumb'];
        $data['filex']=$_POST['filex'];
        $data['summary']=$_POST['summary'];
        $data['author']=$_POST['author'];
        $data['content']=$_POST['content'];
        $data['keywords']=$_POST['keywords'];
        $data['descript']=$_POST['descript'];
        $data['is_home']=$_POST['is_home'];
        $data['is_top']=$_POST['is_top'];
        $data['is_hot']=$_POST['is_hot'];
// return json_encode($data);
        if($data['is_home'] == 'on'){
            $data['is_home']=1;
        }else{
            $data['is_home']=0;
        }
        if($data['is_top'] == 'on'){
            $data['is_top']=1;
        }else{
            $data['is_top']=0;
        }
        if($data['is_hot'] == 'on'){
            $data['is_hot']=1;
        }else{
            $data['is_hot']=0;
        }
        $data['aid']=$this->userMsg['id'];
        if($updid){
            $data['createtime']=time();
            $res=db("channel_cases")->where("id",$updid)->update($data);  
        }else{
            $data['createtime']=time();
            $res=db("channel_cases")->insert($data);
        }

        if(!$res){
            $mess['code']=100;
            $mess['msg']='保存失败';
            return json_encode($mess);
        }    
        $images=$_POST['images'];
        if($updid){
            $product=db("channel_cases")->find($updid);
        }else{
            $product=db("channel_cases")->where($data)->order("id desc")->find();
        }
        
        // $picdata['title']=$album['title'];
        
        $picdata['tid']=$product['id'];
        // $picdata['content']=$album['content'];
        $picdata['aid']=$this->userMsg['id'];
        // $picdata['content']=$product['name'];
        $picdata['tablename']='channel_cases';
        $picture=explode(",",$images);          
        for ($i=0; $i < count($picture); $i++) {            
            $where['id']=$picture[$i];
            $ret=db("attachment")->where($where)->update($picdata); 
        }

        $mess['code']=200;
        $mess['msg']='保存成功';        
        return json_encode($mess);
    }
    public function indexSort(){
        $id=request()->param('id');            
        $sort=request()->param('sort');

        $data['sort']=$sort;
        $res=db("channel_cases")->where("id",$id)->update($data);
        if($res){
            $mess['code']=1;
            $mess['msg']="操作成功";
        }else{
            $mess['code']=0;
            $mess['msg']="操作失败";
        }
        return json_encode($mess);   
    }
    public function default_upload(){
        // $file=$_FILES['file'];
        $file = request()->file('file');
        $fileInfo = $file->getInfo();
        // return json_encode($fileInfo);
        // $post=$_POST;
        // return json_encode($file);
        // print_r($file);exit();
        $mess=$this->upload($file);
        if($mess['filename']){
            $data['filename']=$fileInfo['name'];
            $data['filesize']=$fileInfo['size'];
            $data['filetype']=$fileInfo['type'];
            $data['suffix']=$mess['suffix'];
            $data['urlpath']=$mess['filename'];
            $data['createtime']=time();
            $data['status']=1;
            $res=db("attachment")->insert($data);
            $mess['data']=db("attachment")->where($data)->order("id desc")->find();
        }
        return json_encode($mess);
    }
    public function img_del(){
        $id=$_GET['id'];
        $res=db("attachment")->delete($id);
        if($res){
            $mess['code']=1;
            $mess['msg']="删除成功";
        }else{
            $mess['code']=0;
            $mess['msg']="删除失败";
        }       // print_r($data);exit;
        return json_encode($mess);
    }
}
